package welsir.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import welsir.entity.dos.ArticleComment;
import welsir.entity.dos.SquareComment;
import welsir.entity.vo.ResultMessage;
import welsir.mapper.SquareCommentMapper;
import welsir.mapper.SquareMapper;
import welsir.mapper.UserMapper;
import welsir.service.IArticleCommentService;
import welsir.service.ISquareClassService;
import welsir.service.ISquareCommentService;
import welsir.service.ISquareService;
import welsir.util.ResultUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Genius
 * @date 2023/03/08 20:35
 **/
@RestController
@RequestMapping("/squareComment")
public class SquareCommentController {

    @Resource
    private SquareMapper squareMapper;

    @Autowired
    private ISquareService squareService;

    @Resource
    private SquareCommentMapper squareCommentMapper;

    @Autowired
    private ISquareClassService squareClassService;

    @Resource
    private UserMapper userMapper;

    @Autowired
    private ISquareCommentService squareCommentService;

    @Resource
    private IArticleCommentService articleCommentService;

    @ApiOperation(value = "获取全部圈子评论(分页)")
    @ApiImplicitParam(name = "otherName",value = "otherName",required = true)
    @GetMapping("/getAllSquare/{page}/{limit}")
    public ResultMessage<List<SquareComment>> getAllArticle(
            @PathVariable("page") Integer page,
            @PathVariable("limit") Integer limit
    ) {

        return ResultUtil.data(squareCommentService.VoList(page, limit));
    }

    @ApiOperation(value = "根据id删除圈子评论")
    @ApiImplicitParam(name = "id",value = "文章id",required = true)
    @GetMapping("/deleteSquareById/{id}")
    public ResultMessage<Boolean> deleteSquareById(
            @PathVariable("id") Integer id
    ) {
        //根据id删除
        QueryWrapper<SquareComment> wrapper= new QueryWrapper<SquareComment>();
        wrapper.eq("id",id);
        squareCommentMapper.delete(wrapper);
        return ResultUtil.data(this.squareCommentService.removeById(id));
    }

    @ApiOperation(value = "根据id修改圈子内容评论")
    @ApiImplicitParam(name = "id",value = "文章id",required = true)
    @GetMapping("/ChangeSquareById/{id}/{content}")
    public ResultMessage<Integer> ChangeSquareById(
            @PathVariable("id") Integer id,
            @PathVariable("content") String content
    ) {

        SquareComment squareComment = new SquareComment();
        squareComment.setContent(content);
        QueryWrapper<SquareComment> wrapper= new QueryWrapper<SquareComment>();
        wrapper.eq("id",id);

        return ResultUtil.data(squareCommentMapper.update(squareComment,wrapper));
    }

    @ApiOperation(value = "获得所有评论数量(包括圈子评论和文章评论)")
    @GetMapping("getCommentCount")
    public ResultMessage<Integer> getCommentCount(){
        return ResultUtil.data(squareCommentService.count()+articleCommentService.count());
    }
}
